package com.hao.link;

/**
 * @Description: TODO
 * @author: a'hao
 * @date: 2021/4/18
 */
public class MySingLinkListImpl<T> implements MySingLinkList<T>{
    // 链表的头节点
    private Node<T> firstNode;
    // 链表的大小
    private Integer size = 0;

    public void add(T t) {
        Node<T> newNode = new Node<T>(t, null);
        if (isEmpty()){
            firstNode = newNode;
        }else {
            Node<T> temp = firstNode;
            boolean hasNext = temp.getNextNode() != null;
            while(hasNext){
                temp = temp.getNextNode();
                hasNext = temp.getNextNode() != null;
            }
            temp.setNextNode(newNode);
        }
        size++;
    }

    public void delete(int index) {
        isOut(index);
        // 找到要修改节点的上一个节点
        Node<T> temp = getBeforeByIndex(index);
        temp.setNextNode(temp.getNextNode().getNextNode());
        size--;
    }

    public void update(int index, T t) {
        isOut(index);
        // 找到要删除节点的上一个节点
        Node<T> temp = getBeforeByIndex(index);
        Node<T> updateNode = new Node<T>(t, temp.getNextNode().getNextNode());
        temp.setNextNode(updateNode);

    }

    public void insert(int index, T t) {
        isOut(index);
        // 找到要插入节点的上一个节点
        Node<T> temp = getBeforeByIndex(index);
        Node<T> insertNode = new Node<T>(t, temp.getNextNode());
        temp.setNextNode(insertNode);
        size++;
    }

    public T get(int index) {
        isOut(index);
        // 找到该节点
        Node<T> temp = getBeforeByIndex(index).getNextNode();
        return temp.getT();
    }

    public Integer size() {
        return size;
    }

    public Boolean isEmpty() {
        return size == 0;
    }

    public T getFirst() {
        return firstNode.t;
    }

    private Node<T> getBeforeByIndex(int index){
        isOut(index);
        Node<T> temp = firstNode;
        for (int i = 0; i < index-1; i++) {
            temp = temp.getNextNode();
        }
        return temp;
    }
    private void isOut(int index){
        boolean isOut = index >= size;
        if (isOut){
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    @Override
    public String toString() {
        String out = "";
        Node<T> temp = firstNode;
        for (int i = 0; i < size; i++) {
            out = out + "  " + temp.getT();
            temp = temp.nextNode;
        }
        return out;
    }

    /**
     * @Author a'hao
     * @Description //TODO 这是链表的节点
     * @Date 16:56 2021/4/18
     * @Param
     * @return
     **/
    class Node<T>{
        /**
         * @Author a'hao
         * @Description //TODO 用户要存放的数据
         * @Date 16:57 2021/4/18
         * @Param
         * @return
         **/
        private T t;
        /**
         * @Author a'hao
         * @Description //TODO 下一个节点的地址
         * @Date 16:58 2021/4/18
         * @Param
         * @return
         **/
        private Node<T> nextNode;

        public Node(T t, Node<T> nextNode) {
            this.t = t;
            this.nextNode = nextNode;
        }

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }

        public Node<T> getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node<T> nextNode) {
            this.nextNode = nextNode;
        }
    }
}
